package puzzle.core;

import java.awt.Point;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import puzzle.core.LevelData;
import puzzle.core.Tile;
import puzzle.core.TileFactory;

/** Builds on LevelData to draw and update the tiles. */
public class LevelMap {
  
  public final static int WIDTH = 21;
  public final static int HEIGHT = 14;
  public final static int PIXEL_WIDTH = Tile.WIDTH * LevelMap.WIDTH;
  public final static int PIXEL_HEIGHT = Tile.HEIGHT * LevelMap.HEIGHT;
  
  public static final Color GRID_COLOR = new Color(155, 200, 228);
  
  private LevelData data = null;
  
  public LevelMap() {
    this(new LevelData());
  }
  
  public LevelMap(LevelData data) {
    this.data = data;
    clearNulls();
  }
  
  private void clearNulls() {
    //set all tiles initially to GROUND
    for (int x=0; x<WIDTH; x++) {
      for (int y=0; y<HEIGHT; y++) {
        Tile t = data.getTile(x, y);
        if (t==null)
          data.putTile(TileFactory.getCurrentInstance().getGround(), x, y);
      }
    } //end iteration
  }
  
  public void setData(LevelData d) {
    this.data = d;
    clearNulls();
  }
  
  public LevelData getData() {
    return data;
  }
  
  public void render(Graphics g) {
    //set color
    Color old = g.getColor();
    
    //draw the blue background
    g.setColor(TileFactory.GROUND_COLOR);
    g.fillRect(0, 0, LevelMap.PIXEL_WIDTH, LevelMap.PIXEL_HEIGHT);
    
    //draw each tile
    for (int x=0; x<WIDTH; x++) {
      for (int y=0; y<HEIGHT; y++) {
        Tile t = data.getTile(x, y);
        
        int xpos = x*Tile.WIDTH;
        int ypos = y*Tile.HEIGHT;
        
        //draw the tile
        t.render(g, xpos, ypos);
      }
    }
    
    g.setColor(GRID_COLOR);
    
    int tWidth = LevelMap.PIXEL_WIDTH;
    int tHeight = LevelMap.PIXEL_HEIGHT;
    int tileHeight = Tile.HEIGHT;
    int tileWidth = Tile.WIDTH;
    
    //draw horizontal lines
    for (int i=1; i<HEIGHT; i++) {
      g.drawLine(0, i*tileHeight, tWidth, i*tileHeight);
    }
    
    //draw vertical lines
    for (int i=1; i<WIDTH; i++) {
      g.drawLine(i*tileWidth, 0, i*tileWidth, tHeight);
    }
       
    g.setColor(old);
  }
  
  public void update(int delta) {    
    //update each tile
    for (int x=0; x<WIDTH; x++) {
      for (int y=0; y<HEIGHT; y++) {
        Tile t = data.getTile(x, y);
        t.update(delta);
      }
    }
  }
  
  public Point getIndexOfPosition(float x, float y) {
    int xIndex = (int)x / Tile.WIDTH;
    int yIndex = (int)y / Tile.HEIGHT;
    return new Point(xIndex, yIndex);
  }
  
  public Tile getTile(int x, int y) {
    return data.getTile(x, y);
  }
  
  public void putTile(Tile t, int x, int y) {
    data.putTile(t, x, y);
  }
  
  public int getStartX() {
    return data.getStartX();
  }
  
  public int getStartY() {
    return data.getStartY();
  }
}


/*

protected boolean move(TileType id, float x, float y) {  
  if (id==TileType.GROUND) {
    //draw nothing
  }
  else if (id==TileType.WALL) {
    
  }
  else if (id==TileType.END) {
    //end level
  }
  else if (id==TileType.BOOST || id==TileType.PORT){
    //direction it's facing
    Direction d = null;
    
    //the connector for the port
    Link port = null;
    
    //not wall/ground
    if (t instanceof Directional) {
      d = ((Directional)t).getDirection();
    }
    if (t instanceof Connectable) {
      port = ((Link)t);
    }
    
    if (d!=null) {
      if (p!=null) {
        //is port
      }
      else {
        //is boost
      }
    }
  }
  
  
  
  
  
  //map creation
 
  
  
  
  
    */